<template>
  <el-dialog
    :title="dialogObj.title"
    append-to-body
    :visible.sync="dialogObj.show"
    :close-on-click-modal="false"
    width="600px"
  >
    <el-form
      ref="form"
      status-icon
      :model="form"
      label-width="120px"
      :rules="rules"
      :disabled="dialogObj.read"
      v-if="dialogObj.show"
    >
      <el-form-item label="楼层" prop="venueFloor" v-if="!isHaveFlorr">
        <el-radio-group v-model="form.venueFloor" :disabled="!!dialogObj.id">
          <el-radio
            v-for="(item, index) in floorArr"
            :key="index"
            :label="item.id"
            >{{ item.value }}</el-radio
          >
        </el-radio-group>
      </el-form-item>
      <el-form-item label="公告信息" prop="announcement">
        <el-input
          type="textarea"
          maxlength="500"
          show-word-limit
          :autosize="{ minRows: 9 }"
          v-model="form.announcement"
          style="width:400px;"
          placeholder="请输入公告内容"
        ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="sub" size="mini">确 定</el-button>
      <el-button @click="dialogObj.show = false" size="mini">取 消</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { FLOORARR2 } from "@u/wordbook";
import { validateTemplateContent, validateTemplateFloor } from "@u/validate";
import { _debounce } from "@u/debounce";
import { addAnnouncementApi, updateAnnouncementApi } from "@s/api";
import Cookie from "@u/cookie";
export default {
  name: "dialogCom",
  props: ["dialogObj"],
  data() {
    return {
      floorArr: FLOORARR2,
      isHaveFlorr:
        JSON.parse(localStorage.getItem("mb_userInfo")).venueFloor == 0
          ? ""
          : JSON.parse(localStorage.getItem("mb_userInfo")).venueFloor, // 是否有楼层
      form: {
        announcement: "",
        venueFloor: ""
      },
      rules: {
        announcement: [
          { required: true, message: "请输入公告信息", trigger: "blur" },
          { validator: validateTemplateContent }
        ],
        venueFloor: [
          { required: true, message: "请选择楼层", trigger: "blur" },
          { validator: validateTemplateFloor }
        ]
      }
    };
  },
  methods: {
    initDialog() {
      if (this.dialogObj.id) {
        this.form.announcement = this.dialogObj.form.announcement;
        this.form.venueFloor = Number(this.dialogObj.form.venueFloor);
      } else {
        this.form.announcement = "";
        let venueFloorNum =
          JSON.parse(localStorage.getItem("mb_userInfo")).venueFloor || "";
        if (Number(venueFloorNum) !== 0) {
          this.form.venueFloor = Number(
            JSON.parse(localStorage.getItem("mb_userInfo")).venueFloor
          );
        } else if (Number(venueFloorNum) == 0) {
          this.form.venueFloor = "";
        }
      }
    },
    sub: _debounce(function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.dialogObj.id) {
            this.updateSub();
          } else {
            this.addSub();
          }
        }
      });
    }),
    addSub() {
      let json = {
        announcement: this.form.announcement,
        merchantType: 1, // 1 餐饮
        venueFloor: this.form.venueFloor
      };
      addAnnouncementApi(json).then(() => {
        this.dialogObj.show = false;
        this.$emit("reflash");
        this.$message({
          type: "success",
          message: "添加成功！"
        });
      });
    },
    updateSub() {
      let json = {
        id: this.dialogObj.form.id,
        announcement: this.form.announcement,
        merchantType: 1, // 1 餐饮,
        venueFloor: this.form.venueFloor
      };
      updateAnnouncementApi(json).then(() => {
        this.dialogObj.show = false;
        this.$emit("reflash");
        this.$message({
          type: "success",
          message: "修改成功！"
        });
      });
    }
  },
  watch: {
    "dialogObj.show"(val) {
      if (val) {
        this.initDialog();
      }
    }
  }
};
</script>
<style scoped lang="scss"></style>
